package com.bamboo.framework.framework.security.service.impl;

import com.bamboo.framework.framework.security.entity.Menu;
import com.bamboo.framework.framework.security.entity.Role;
import com.bamboo.framework.framework.security.mapper.MenuMapper;
import com.bamboo.framework.framework.security.service.IMenuService;
import com.bamboo.framework.common.format.ReturnMsg;
import com.bamboo.framework.common.enums.Status;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author 郭晴
 * @since 2018-12-19
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getMenusByRoles(List<Role> roles) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("roles",roles);
        return  menuMapper.selectMenusByParameter(map);
    }

    @Override
    public List<Menu> selectMenusAll() {
        return menuMapper.selectMenusAll();
    }

    @Override
    public boolean deleteMenuById(Integer id) {
        int i = menuMapper.deleteById(id);
        if(i<1){
            return false;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnMsg saveMenu(Menu menu) {
        if(menu==null|| StringUtil.isEmpty( menu.getTitle())){
          return  new ReturnMsg(Status.PARAMETER_NULL,"参数空值，标题不能为空");
        }
        if(menu.getParentId()==null){
          menu.setParentId(0);
        }
        if(StringUtil.isEmpty(menu.getFlag())){
          menu.setFlag("0");
        }
        boolean saveOrUpdateMenu = this.saveOrUpdate(menu);
      if(saveOrUpdateMenu){
        return new ReturnMsg(Status.SUCCESS,"更新或插入菜单成功");
      }
      return  new ReturnMsg(Status.ERROR,"更新或插入菜单失败");
    }

  @Override
  public List<Menu> selectSubmenuAll() {
    return menuMapper.selectSubmenuAll();
  }


}
